package com.runa.monitor.platinterface.netservice.equipment.dtu;

import com.runa.protocol.analysis.*;
import com.runa.protocol.util.ByteUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 2024/8/27 15:46
 * zhangYB
 */
@Slf4j
public class DtuBufferAnalysis extends ShellBufferAnalysis<byte[]> {

    private ShellBufferAnalysis hdBufferAnalysis;

    private ShellBufferAnalysis dtu86BufferAnalysis;
    private ShellBufferAnalysis cmBufferAnalysis;
    private ShellBufferAnalysis urBufferAnalysis;

    public DtuBufferAnalysis(MetaBufferAnalysis metaBufferAnalysis) {
        super(metaBufferAnalysis, 0);
    }

    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        if (isHdProtocol(data, length)) {
            return getHdBufferAnalysis().fill(data, length, netEquNo);
        } else if (is86Protocol(data, length)) {
            return get86BufferAnalysis().fill(data, length, netEquNo);
        } else if (isCMProtocol(data, length)) {
            return getCMBufferAnalysis().fill(data, length, netEquNo);
        } else if (isURProtocol(data, length)) {
            return getURBufferAnalysis().fill(data, length, netEquNo);
        } else {
            log.info("DTU未知报文: {}", ByteUtil.byteToHexString(data, 0, length));
        }
        return 0;
    }

    private boolean isURProtocol(byte[] data, int length) {
        return false;
    }

    private DataAnalysis<byte[]> getURBufferAnalysis() {
        if (null == urBufferAnalysis) {
            urBufferAnalysis = new DtuURBufferAnalysis(analysis);
        }
        return urBufferAnalysis;
    }

    private boolean isCMProtocol(byte[] data, int length) {
        return false;
    }

    private DataAnalysis<byte[]> getCMBufferAnalysis() {
        if (null == cmBufferAnalysis) {
            cmBufferAnalysis = new DtuCMBufferAnalysis(analysis, 0);
        }
        return cmBufferAnalysis;
    }


    private boolean is86Protocol(byte[] data, int length) {
        int begin;
        for (begin = 0; begin < length; begin++) {
            if ((data[begin]&0xff) == 0x5B) {
                if ((data[begin + 1]&0xff) == 0x5B) begin++;
                break;
            }
        }
        int end;
        for (end = begin; end < length; end++) {
            if ((data[end]&0xff) == 0x5B) break;
        }
        if (end - begin < 7) {
            return false;
        }
        return true;
    }

    private ShellBufferAnalysis get86BufferAnalysis() {
        if (null == dtu86BufferAnalysis) {
            dtu86BufferAnalysis = new Dtu86BufferAnalysis(analysis);
        }
        return dtu86BufferAnalysis;
    }

    private boolean isHdProtocol(byte[] data, int length) {
        int begin;
        for (begin = 0; begin < length; begin++) {
            if ((data[begin] & 0xff) == 0x7B) {
                if ((data[begin + 1] & 0xff) == 0x7B) {
                    begin++;
                }
                break;
            }
        }
        if (length < begin + 4) {
            return false;
        }
        int len = ((data[begin + 2] & 0xff) << 8) + (data[begin + 3] & 0xff);
        if (length < len) {
            return false;
        }
        if ((data[begin + len - 1] & 0xff) != 0x7B) {
            return false;
        }
        return true;
    }

    private ShellBufferAnalysis getHdBufferAnalysis() {
        if (null == hdBufferAnalysis) {
            hdBufferAnalysis = new DtuBufferAnalysis(analysis);
        }
        return hdBufferAnalysis;
    }



}
